package com.easy.slurm.advice;

import com.easy.slurm.exception.ResultCode;
import com.easy.slurm.vo.ResultVo;
import com.easy.slurm.exception.BussinessException;
import com.easy.slurm.exception.RightException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;

@RestControllerAdvice
@Slf4j
public class GlobalResponseEntityExceptionHandler extends ResponseEntityExceptionHandler {

    @ExceptionHandler(Exception.class)
    protected ResultVo<Void> handlerNotRoleException(Exception ex) {
        log.error("全局异常日志:{}", ex);
        return ResultVo.fail(ResultCode.FAILURE);
    }

    /**
     * 业务异常
     * @param ex
     * @return
     */
    @ExceptionHandler(BussinessException.class)
    protected ResultVo<Void> handlerBussinessException(BussinessException ex) {
        log.info("业务异常{}", ex);
        ResultCode resultCode = ex.getResultCode();
        return ResultVo.fail(resultCode);
    }

    /**
     * 权限异常
     * @param exception
     * @return
     */
    @ExceptionHandler(RightException.class)
    public ResultVo<Void> RightException(RightException exception) {
        return ResultVo.fail(ResultCode.LOGINFAILURE);
    }
}
